/*
 * Controles.java
 *
 * Created on September 18, 2007, 6:21 PM
 */

package br.org.codeforlife.exemplo.jogo.forca;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author  diego
 */
public class Controles extends javax.swing.JPanel {
    
    private JFrame framePai;
    
    private Character letraCorrente;
    
    
    private String palavra = "TESTE";
    
    private StringBuffer palavraLabel;
    private String secretaLabel[] = new String[1];
    
    private ArrayList<Character> palavraAL = new ArrayList<Character>();
    private ArrayList<Character> secretaAL = new ArrayList<Character>();
    
    private int erros = 0;
    
    /** Creates new form Controles */
    public Controles(JFrame pai) {
        initComponents();
        
       try {
            
            
            File arquivo = new File(getClass().getResource("/forca/PALAVRA.TXT").toURI());
            FileReader fr = new FileReader(arquivo);
            BufferedReader bfrr = new BufferedReader(fr);
            
            palavra = bfrr.readLine();
            if (palavra == null)
                palavra = "DINOSSAURO";
                 
            
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(palavra);
        
        framePai = pai;
        
        secretaLabel[0] = palavra;
        palavraLabel = new StringBuffer();
        palavraLabel.setLength(secretaLabel[0].length());
        
        this.jPasswordFieldSecreta.setText(secretaLabel[0].toString());
        
        
        for (int i = 0; i < palavra.length(); ++i){
            palavraAL.add((Character)palavra.charAt(i));
            secretaAL.add((Character)palavra.charAt(i));
        }
        
        
        
        this.jButtonA.addActionListener(new Letras());
        this.jButtonB.addActionListener(new Letras());
        this.jButtonC.addActionListener(new Letras());
        this.jButtonD.addActionListener(new Letras());
        this.jButtonE.addActionListener(new Letras());
        this.jButtonF.addActionListener(new Letras());
        this.jButtonG.addActionListener(new Letras());
        this.jButtonH.addActionListener(new Letras());
        this.jButtonI.addActionListener(new Letras());
        this.jButtonJ.addActionListener(new Letras());
        this.jButtonL.addActionListener(new Letras());
        this.jButtonM.addActionListener(new Letras());
        this.jButtonN.addActionListener(new Letras());
        this.jButtonO.addActionListener(new Letras());
        this.jButtonP.addActionListener(new Letras());
        this.jButtonQ.addActionListener(new Letras());
        this.jButtonR.addActionListener(new Letras());
        this.jButtonS.addActionListener(new Letras());
        this.jButtonT.addActionListener(new Letras());
        this.jButtonU.addActionListener(new Letras());
        this.jButtonV.addActionListener(new Letras());
        this.jButtonX.addActionListener(new Letras());
        this.jButtonZ.addActionListener(new Letras());
        
    }
    
    public Character retornaLetra(){
        
        return letraCorrente;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jButtonA = new javax.swing.JButton();
        jButtonB = new javax.swing.JButton();
        jButtonC = new javax.swing.JButton();
        jButtonD = new javax.swing.JButton();
        jButtonE = new javax.swing.JButton();
        jButtonF = new javax.swing.JButton();
        jButtonG = new javax.swing.JButton();
        jButtonH = new javax.swing.JButton();
        jButtonI = new javax.swing.JButton();
        jButtonJ = new javax.swing.JButton();
        jButtonL = new javax.swing.JButton();
        jButtonM = new javax.swing.JButton();
        jButtonN = new javax.swing.JButton();
        jButtonO = new javax.swing.JButton();
        jButtonP = new javax.swing.JButton();
        jButtonQ = new javax.swing.JButton();
        jButtonR = new javax.swing.JButton();
        jButtonS = new javax.swing.JButton();
        jButtonT = new javax.swing.JButton();
        jButtonU = new javax.swing.JButton();
        jButtonV = new javax.swing.JButton();
        jButtonX = new javax.swing.JButton();
        jButtonZ = new javax.swing.JButton();
        jLabelSecreta = new javax.swing.JLabel();
        jTextFieldPalavraAchada = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelAchado = new javax.swing.JLabel();
        forca1 = new br.org.codeforlife.exemplo.jogo.forca.Forca();
        jPasswordFieldSecreta = new javax.swing.JPasswordField();

        jFormattedTextField1.setText("jFormattedTextField1");

        setLayout(new java.awt.GridBagLayout());

        jButtonA.setText("A");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonA, gridBagConstraints);

        jButtonB.setText("B");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonB, gridBagConstraints);

        jButtonC.setText("C");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonC, gridBagConstraints);

        jButtonD.setText("D");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonD, gridBagConstraints);

        jButtonE.setText("E");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonE, gridBagConstraints);

        jButtonF.setText("F");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonF, gridBagConstraints);

        jButtonG.setText("G");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonG, gridBagConstraints);

        jButtonH.setText("H");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonH, gridBagConstraints);

        jButtonI.setText("I");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonI, gridBagConstraints);

        jButtonJ.setText("J");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonJ, gridBagConstraints);

        jButtonL.setText("L");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonL, gridBagConstraints);

        jButtonM.setText("M");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonM, gridBagConstraints);

        jButtonN.setText("N");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonN, gridBagConstraints);

        jButtonO.setText("O");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonO, gridBagConstraints);

        jButtonP.setText("P");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonP, gridBagConstraints);

        jButtonQ.setText("Q");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonQ, gridBagConstraints);

        jButtonR.setText("R");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonR, gridBagConstraints);

        jButtonS.setText("S");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonS, gridBagConstraints);

        jButtonT.setText("T");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonT, gridBagConstraints);

        jButtonU.setText("U");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonU, gridBagConstraints);

        jButtonV.setText("V");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonV, gridBagConstraints);

        jButtonX.setText("X");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonX, gridBagConstraints);

        jButtonZ.setText("Z");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jButtonZ, gridBagConstraints);

        jLabelSecreta.setText("Secreta:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        add(jLabelSecreta, gridBagConstraints);

        jTextFieldPalavraAchada.setColumns(7);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        add(jTextFieldPalavraAchada, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
        add(jSeparator1, gridBagConstraints);

        jLabelAchado.setText("Achado:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        add(jLabelAchado, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 9, 0);
        add(forca1, gridBagConstraints);

        jPasswordFieldSecreta.setText("jPasswordField1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(jPasswordFieldSecreta, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    private class Letras implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            letraCorrente = (Character)((JButton)e.getSource()).getText().charAt(0);
            if(palavraAL.contains(letraCorrente)){
                
                System.out.println("Achou");
                int indice;
                while ((indice = secretaAL.indexOf(letraCorrente)) != -1){
                    indice = secretaAL.indexOf(letraCorrente);
                    System.out.println(indice);
                    palavraLabel.setCharAt(indice, letraCorrente.charValue());
                    
                    secretaAL.set(indice,' ');
                    
                }
                jTextFieldPalavraAchada.setText(palavraLabel.toString());
                if (palavraLabel.toString().equals(palavra))
                    JOptionPane.showMessageDialog(null,"VOCE GANHOU", "MENSAGEM", JOptionPane.INFORMATION_MESSAGE);
                
                
            }else{
                ++erros;
                if(erros < 8){
                    if (erros == 1)
                        forca1.getJLabelCabeca().setForeground(Color.RED);
                    if (erros == 2)
                        forca1.getJLabelTronco().setForeground(Color.RED);
                    if (erros == 3)
                        forca1.getJLabelBracoEsquerdo().setForeground(Color.RED);
                    if (erros == 4)
                        forca1.getJLabelBracoDireito().setForeground(Color.RED);
                    if (erros == 5)
                        forca1.getJLabelCintura().setForeground(Color.RED);
                    if (erros == 6)
                        forca1.getJLabelPernaEsquerda().setForeground(Color.RED);
                    if (erros == 7)
                        forca1.getJLabelPernaDireita().setForeground(Color.RED);
                    
                }else{
                    JOptionPane.showMessageDialog(null,"VOCE PERDEU", "MENSAGEM", JOptionPane.ERROR_MESSAGE);
                    jTextFieldPalavraAchada.setText("");
                    erros = 0;
                    forca1.getJLabelCabeca().setForeground(Color.BLACK);
                    forca1.getJLabelTronco().setForeground(Color.BLACK);
                    forca1.getJLabelBracoEsquerdo().setForeground(Color.BLACK);
                    forca1.getJLabelBracoDireito().setForeground(Color.BLACK);
                    forca1.getJLabelCintura().setForeground(Color.BLACK);
                    forca1.getJLabelPernaEsquerda().setForeground(Color.BLACK);
                    forca1.getJLabelPernaDireita().setForeground(Color.BLACK);
                    
                }
            }
            framePai.validate();
            
        }
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private br.org.codeforlife.exemplo.jogo.forca.Forca forca1;
    private javax.swing.JButton jButtonA;
    private javax.swing.JButton jButtonB;
    private javax.swing.JButton jButtonC;
    private javax.swing.JButton jButtonD;
    private javax.swing.JButton jButtonE;
    private javax.swing.JButton jButtonF;
    private javax.swing.JButton jButtonG;
    private javax.swing.JButton jButtonH;
    private javax.swing.JButton jButtonI;
    private javax.swing.JButton jButtonJ;
    private javax.swing.JButton jButtonL;
    private javax.swing.JButton jButtonM;
    private javax.swing.JButton jButtonN;
    private javax.swing.JButton jButtonO;
    private javax.swing.JButton jButtonP;
    private javax.swing.JButton jButtonQ;
    private javax.swing.JButton jButtonR;
    private javax.swing.JButton jButtonS;
    private javax.swing.JButton jButtonT;
    private javax.swing.JButton jButtonU;
    private javax.swing.JButton jButtonV;
    private javax.swing.JButton jButtonX;
    private javax.swing.JButton jButtonZ;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabelAchado;
    private javax.swing.JLabel jLabelSecreta;
    private javax.swing.JPasswordField jPasswordFieldSecreta;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextFieldPalavraAchada;
    // End of variables declaration//GEN-END:variables
    
}
